/* eslint-disable no-lonely-if */
import React, { PureComponent } from 'react';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Modal, Row, Col, Input, Select, Button } from 'antd';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import DiffTable from './DiffTable';

const { confirm } = Modal;
const FormItem = Form.Item;
const { Option } = Select;
@Form.create()
class DiffModal extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {}

  handleCancel = () => {
    const { onCancel } = this.props;
    onCancel();
  };

  computed = () => {
    const {
      form,
      computed,
      diffModalData: { insDifferentDataId, accuDifferentDataId, handleSource },
      assType,
    } = this.props;
    form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        if (Number(assType) === 1) {
          if (this.isPerIdeDel(values)) {
            computed({ ...values, insDifferentDataId });
          } else {
            computed({ ...values, insDifferentDataId, perIde: handleSource[0].perIde });
          }
        } else {
          computed({ ...values, accuDifferentDataId });
        }
      }
    });
  };

  showEndConfirm = values => {
    const {
      onOk,
      diffModalData: { insDifferentDataId, accuDifferentDataId },
      assType,
    } = this.props;
    confirm({
      title: '生成一次性费用',
      okText: '确定',
      icon: <ExclamationCircleOutlined />,
      cancelText: '取消',
      content: '确定生成一次性费用吗？',
      onOk: () => {
        if (Number(assType) === 1) {
          onOk({ ...values, insDifferentDataId });
        } else {
          onOk({ ...values, accuDifferentDataId });
        }
      },
      onCancel: () => {},
    });
  };

  showFeeConfirm = values => {
    confirm({
      title: '差异处理',
      okText: '生成一次性费用',
      cancelText: '取消',
      icon: <ExclamationCircleOutlined />,
      content: (
        <div>
          当前险种<span style={{ color: '#FAAD14' }}>有差异</span>
          ，若不调整成无差异，将会生成一次性费用，生成后不能再调整差异。
        </div>
      ),
      onOk: () => {
        this.showEndConfirm(values);
      },
      onCancel: () => {},
    });
  };

  showNoFeeConfirm = values => {
    const { onOk } = this.props;
    confirm({
      title: '差异处理',
      okText: '确定',
      cancelText: '取消',
      icon: <ExclamationCircleOutlined />,
      content: (
        <div>
          当前险种<span style={{ color: '#FAAD14' }}>无差异</span>
          ，确认完成差异处理吗？
        </div>
      ),
      onOk: () => {
        onOk(values);
      },
      onCancel: () => {},
    });
  };

  // 缴费人员类别是否已删除
  renderPerIde = () => {
    const {
      diffModalData: { handleSource, joinIdeList },
    } = this.props;
    let isPerIde = false;
    isPerIde = joinIdeList.some(item => item.ideId === handleSource[0] && handleSource[0].perIde);
    return isPerIde;
  };

  // 提交时缴费人员类别是否已删除
  isPerIdeDel = values => {
    const {
      diffModalData: { joinIdeList },
    } = this.props;
    let isPerIde = false;
    // 下拉列表中是否已删除返回值(缴纳人员类别)
    isPerIde = joinIdeList.some(item => item.ideId === values.perIde);
    return isPerIde;
  };

  handleOk = () => {
    const {
      form,
      diffModalData: { insDifferentDataId, isCreateFee, accuDifferentDataId, handleSource },
      assType,
    } = this.props;

    form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        // 下拉列表中是否已删除返回值(参保类型)
        if (Number(isCreateFee) === 1) {
          if (Number(assType) === 1) {
            if (this.isPerIdeDel(values)) {
              this.showFeeConfirm({
                ...values,
                isCreateFee,
                insDifferentDataId,
              });
            } else {
              this.showFeeConfirm({
                ...values,
                isCreateFee,
                insDifferentDataId,
                perIde: handleSource[0].perIde,
              });
            }
          } else {
            this.showFeeConfirm({
              ...values,
              isCreateFee,
              accuDifferentDataId,
            });
          }
        } else {
          if (Number(assType) === 1) {
            if (this.isPerIdeDel(values)) {
              this.showNoFeeConfirm({
                ...values,
                isCreateFee,
                insDifferentDataId,
              });
            } else {
              this.showNoFeeConfirm({
                ...values,
                isCreateFee,
                insDifferentDataId,
                perIde: handleSource[0].perIde,
              });
            }
          } else {
            this.showNoFeeConfirm({
              ...values,
              isCreateFee,
              accuDifferentDataId,
            });
          }
        }
      }
    });
  };

  render() {
    const {
      diffModalData: {
        diffVisible,
        handleSource,
        detailSource,
        pkgData,
        joinIdeList,
        diffState,
        sumDiffAmt,
      },
      form: { getFieldDecorator },
      loading,
      assType,
    } = this.props;
    // 表格配置 -
    // console.log('diffModalData', this.props.diffModalData);
    const formItemLayout = {
      labelCol: { md: { span: 8 } },
      wrapperCol: { md: { span: 10 } },
    };
    console.log('eeee',handleSource);
    const renderFooter = () => {
      if (Number(diffState) === 1 || Number(diffState) === 2) {
        return [
          <Button key="back" onClick={this.handleCancel}>
            关闭
          </Button>,
        ];
      }
      return [
        <Button key="back" onClick={this.handleCancel}>
          取消
        </Button>,
        <Button key="submit" type="primary" onClick={this.handleOk}>
          处理
        </Button>,
      ];
    };
    return (
      <Modal
        title="差异处理"
        visible={diffVisible}
        // onOk={this.handleOk}
        onCancel={this.handleCancel}
        destroyOnClose
        width={800}
        footer={renderFooter()}
      >
        <div>
          <DiffTable
            handleSource={handleSource}
            detailSource={detailSource}
            loading={loading}
            assType={assType}
            sumDiffAmt={sumDiffAmt}
          />
          {Number(diffState) === 3 ? (
            <Form>
              <Row style={{ marginTop: 30 }}>
                <Col span={18}>
                  <FormItem {...formItemLayout} label="预缴申报工资 变更">
                    {getFieldDecorator('payWage', {
                      rules: [{ required: true, message: '请填写申报工资' }],
                      initialValue: handleSource[0] && handleSource[0].payWage,
                    })(<Input placeholder="请填写申报工资" />)}
                  </FormItem>
                  {Number(assType) === 1 ? (
                    <FormItem {...formItemLayout} label="预缴社保组合 变更">
                      {getFieldDecorator('insGrpId', {
                        rules: [{ required: true, message: '请选择预缴社保组合' }],
                        initialValue: handleSource[0] && handleSource[0].insGrpId,
                      })(
                        <Select placeholder="请选择" style={{ width: '100%' }}>
                          {pkgData.map(value => (
                            <Option value={value.insLsPkgId}>{value.insLsPkgName}</Option>
                          ))}
                        </Select>
                      )}
                    </FormItem>
                  ) : (
                    <FormItem {...formItemLayout} label="预缴公积金组合 变更">
                      {getFieldDecorator('accuGrpId', {
                        rules: [{ required: true, message: '请选择预缴公积金组合' }],
                        initialValue: handleSource[0] && handleSource[0].accuGrpId,
                      })(
                        <Select placeholder="请选择" style={{ width: '100%' }}>
                          {pkgData.map(value => (
                            <Option value={value.accuLsPkgId}>{value.accuLsPkgName}</Option>
                          ))}
                        </Select>
                      )}
                    </FormItem>
                  )}
                  {Number(assType) === 1 && handleSource[0] && handleSource[0].perIde ? (
                    <FormItem {...formItemLayout} label="缴费人员类别 变更">
                      {getFieldDecorator('perIde', {
                        rules: [{ required: true, message: '请选择缴费人员类别' }],
                        initialValue: handleSource[0] && handleSource[0].perIde,
                        // initialValue: this.renderPerIde()
                        //   ? handleSource[0] && handleSource[0].perIde
                        //   : handleSource[0] && handleSource[0].perIdeName,
                      })(
                        <Select placeholder="请选择" style={{ width: '100%' }}>
                          {joinIdeList.map(value => (
                            <Option value={value.ideId}>{value.ideName}</Option>
                          ))}
                        </Select>
                      )}
                    </FormItem>
                  ) : null}
                  <Col offset={8}>
                    <Button type="primary" onClick={this.computed}>
                      计算
                    </Button>
                  </Col>
                </Col>
              </Row>
            </Form>
          ) : null}
        </div>
      </Modal>
    );
  }
}

export default DiffModal;
